<template>
	<view class="content">
		<view class="title">
			<view class="tips">学包名称：</view>
			<view class="name">{{package_name}}</view>
		</view>
		<view class="cont">
			<view class="tips">发布到：</view>
			<view>
				<checkbox-group @change="checkboxChange">
					<label v-for="(item,index) in childList" :key="index">
						<checkbox :value="item.student_id" />{{item.nicheng}}
					</label>
				</checkbox-group>
			</view>
		</view>
		<view class="btn" @click="release()">确认发布</view>
	</view>
</template>

<script>
	import util from '@/common/util.js'
	export default {
		data() {
			return {
				unid: '',
				package_name: '',
				childList: [],
				student_id: ''
			}
		},
		onLoad(e) {
			this.unid = e.unid;
			this.package_name = e.package_name;
			this.getChild();
		},
		methods: {
			// 获取孩子列表
			getChild() {
				this.$zapi.apiPost('user/currency.Parents/childlist').then((res) => {
					if (res.data.code == 200) {
						var list = res.data.data;
						for (var i = 0; i < list.length; i++) {
							if (list[i].avatar == '') {
								list[i].avatar = this.defimg;
							}
							if (list[i].nicheng == '') {
								list[i].nicheng = "未设置昵称";
							}
							list[i].gradeName = util.getgradeName(list[i].grade);
						}
						this.childList = list;
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
					}
				})
			},
			checkboxChange: function(e) {
				this.student_id = e.target.value;
			},
			release() {
				var usr_msg = {};
				this.childList.forEach((item, index) => {
					if (this.student_id.indexOf(item.student_id) >= 0) {
						usr_msg[item.student_id] = {
							usr_id: item.student_id,
							usr_head: item.avatar,
							usr_name: item.nicheng
						}
					}
				});

				var data = {
					usr_id: this.student_id.join(','),
					package_id: this.unid,
					send_usr_id: uni.getStorageSync('userInfo').id,
					usr_msg: JSON.stringify(usr_msg)
				}
				this.$papi.apiPost('adminapi/moblie/stu/pack/add', data).then(res => {
					if (res.data.code == 200) {
						setTimeout(function() {
							uni.navigateBack({
								delta: 1
							})
						}, 1500);
					} else {
						uni.showToast({
							title: '请勿重复发布',
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #FFFFFF;
	}

	.content {
		margin: 0 30upx;
	}

	.title {
		display: flex;
		justify-content: flex-start;
		min-height: 60upx;
		padding: 16upx 0;
		border-bottom: 2upx solid #E5E5E5;

		view {
			color: #333333;
			font-size: 34upx;
			line-height: 60upx;
		}
	}

	.tips {
		color: #333333;
		font-size: 34upx;
		line-height: 60upx;
		width: 160upx;
		white-space: nowrap;
		text-align: right;
		margin-right: 30upx;
	}

	.cont {
		display: flex;
		justify-content: flex-start;
		margin-top: 30upx;
	}

	label {
		display: block;
		line-height: 60upx;
		margin-bottom: 40upx;
		color: #333333;
		font-size: 32upx;
	}

	checkbox {
		transform: scale(0.75);
	}

	.wx-checkbox-input {
		border: 2upx solid #999999 !important;
		border-radius: 50% !important;
	}

	.wx-checkbox-input.wx-checkbox-input-checked {
		border: 2upx solid #FF9900 !important;
		background: #FF9900 !important;
	}

	.wx-checkbox-input.wx-checkbox-input-checked:before {
		color: #FFFFFF;
	}

	.btn {
		height: 80upx;
		line-height: 80upx;
		text-align: center;
		color: #FFFFFF;
		font-size: 34upx;
		position: fixed;
		bottom: 30upx;
		left: 30upx;
		width: 690upx;
		background-color: #FF9900;
		border-radius: 8upx;
	}
</style>
